using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Threading;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Net;
namespace NetMonitor.Things
{
    class InfoThing : Thing
    {
        protected override int ping()
        {
            Byte[] data = System.Text.Encoding.ASCII.GetBytes("CPU");
            Byte[] reply = new Byte[512];
            try
            {
                TcpClient client = new TcpClient(hostname, 9000);
                if (!client.Connected)
                    return -3;
                client.Client.SendTimeout = timeOut;
                client.Client.Send(data, SocketFlags.None);
                client.Client.ReceiveTimeout = timeOut;
                if (client.Client.Receive(reply, SocketFlags.None) <= 0)
                    return -4;

                client.Close();
            }
            catch (ArgumentNullException e)
            {
                Console.WriteLine("ArgumentNullException: {0}", e);
                return -1;
            }
            catch (SocketException e)
            {
                Console.WriteLine("SocketException: {0}", e);
                return -2;
            }
            int iRet=-6;
            int.TryParse(Encoding.ASCII.GetString(reply), out iRet);


            return iRet;
        }

        public override void drawThing(Graphics g)
        {
            
            Pen pen = new Pen(col);
            float o, penWidth = 14;

            o = (float)penWidth / 2.0f;

            SizeF stringSize = new SizeF();
            stringSize = g.MeasureString(getText(), font);
            boxW = stringSize.Width;
            boxH = stringSize.Height;
            boxX = w / 2 + x - boxW / 2;
            boxY = y;

            pen.EndCap = LineCap.Triangle;
            pen.StartCap = LineCap.Triangle;
            pen.Color = Color.FromArgb(255, col);
            pen.Width = 24;

            g.FillRectangle(new SolidBrush(col), x, y, w, h);

            //DataTable dt = getHistory();
            pen = new Pen(Color.Black, 2);
           // int xi = x;
            for (int i = 0; i < history.Count - 1; i++)
            {
                pen = new Pen(Color.Black, 2);
                if (history[i + 1] >= 0 && history[i] >= 0)
                    g.DrawLine(pen,
                        x+(i * w / historySize), calcY(history[i]),
                        x+((i + 1) * w / historySize), calcY(history[i + 1]));
            }

            g.FillRectangle(new SolidBrush(Color.White), boxX, boxY, boxW, boxH);
            g.DrawRectangle(new Pen(boxColor, 3), boxX, boxY, boxW, boxH);
            g.DrawString(getText(), font, new SolidBrush(col), new PointF(boxX, boxY));

            //labelLast.Text = history[history.Count - 1].ToString();
            StringFormat stringFormat = new StringFormat();
            stringFormat.Alignment = StringAlignment.Center;
            stringFormat.LineAlignment = StringAlignment.Center;

            if (history.Count > 0)
                g.DrawString(history[history.Count - 1].ToString(), new Font("Tahoma", 32), Brushes.Black, new PointF(x+w/2, y+48),stringFormat);
       
        }

        private int calcY(int _val)
        {
            return (int)y + (int)h - _val*h/max ;
        }
    }
}
